package com.saudidentalapp;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.saudiapp.webservice.Constants;
import com.saudiapp.webservice.Responce;
import com.saudiapp.webservice.Utilities;
import com.saudiapp.webservice.WebServiceManager;
import com.saudiapp.webservice.WebServiceManager.WebServiceListener;
import com.saudidentalapp.datamodels.Committee;
import com.saudidentalapp.datamodels.Poster;

public class OrganizingCommittee extends Activity implements WebServiceListener,OnItemClickListener
{
	ListView lstPosters;
	Utilities utilities;
	
	 @Override
	protected void onCreate(Bundle savedInstanceState) 
	 {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_organizing_committee);
		lstPosters=(ListView)findViewById(R.id.xlstPosters);
		setView();
	}
	 
	private void setView()
	{
		utilities=Utilities.getInstance(OrganizingCommittee.this);
		utilities.showProgreessDialog();
		if(utilities.haveNetworkConnection())
		{
			WebServiceManager webServiceManager=new WebServiceManager(this);
			webServiceManager.setonWebServiceListener(this);
			webServiceManager.getOrgCommitee();
		}
		else
		{
		utilities.dismiss();
		utilities.showToast(Constants.INTERNET_NOT_AVAILABELE);
		}
		
		
		final Activity activity=this;
		ImageView imgBack=(ImageView)findViewById(R.id.xbtnVwBack);
		imgBack.setOnClickListener(new OnClickListener() 
		{
			
			@Override
			public void onClick(View arg0) 
			{
				finish();
				startActivity(new Intent(activity,TabActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				
			}
		});
			
	}

	@Override
	public void onProcesCompleted(int requestId, Object Data) 
	{	
		utilities.dismiss();			
		PosterAdapter adapter=new PosterAdapter(OrganizingCommittee.this, R.layout.custom_poster_row,((ArrayList<Committee>)((Responce)Data).getResponceData()));
		if(adapter!=null)
		{
			lstPosters.setAdapter(adapter);
			/*ArrayList<Poster>programs=new ArrayList<Poster>();
			programs=(ArrayList<Poster>)((Responce)Data).getResponceData();
			for(int i=0;i<programs.size();i++)
			{
				if(!isProgramInSearchList(programs.get(i)))
				{
					Constants.Data.add(programs.get(i));
				}
			}*/
		}
			
		lstPosters.setOnItemClickListener(this);
	}
	
	boolean isProgramInSearchList(Poster program)
	{
		boolean result=false;
		for(int i=0;i<Constants.Data.size();i++)
		{
			Object obect=Constants.Data.get(i);
			if(obect instanceof Poster)
			{
				if(((Poster)Constants.Data.get(i)).getID()==program.getID())
				{
					result=true;
					break;
				}
				else
				{
					result=false;
				}
			}
			else
			{
				result=false;
			}
		}
		
		return result;
	}

	@Override
	public void onErrorOccured(int requestId) 
	{
		utilities.dismiss();	
		utilities.showToast("Error Occured Please try Later...");
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) 
	{
		/*Intent aboutSpeakerIntent=new Intent(OrganizingCommittee.this, ActivityAboutPoster.class);	
		Poster speaker=(Poster)arg0.getAdapter().getItem(position);
		aboutSpeakerIntent.putExtra("MyClass",speaker);  
		startActivity(aboutSpeakerIntent);*/
		
	}
}

